const app = getApp();
import req from '../../utils/Request.js';
import appReq from '../../utils/AppRequest.js';

Page({ 
  data: {
    keyId: '',
    movies: [],
    list: null,
    houseList: null,
    messageList: null,
    phoneNumber: null,
    attentionStatus: false,
    collectId: "",
    shareCustomerId: "",
  },

  onLoad: function (value) {
    console.log(value)

    this.setData({
      keyId: value.keyId,
    })
    console.log(value.shareCustomerId)
    if (value.shareCustomerId) {
      app.globalData.shareCustomerId = value.shareCustomerId
    }
    this.getShareId();
    this.getDetail();
    this.getAttention();
  },

  previewImg: function (e) {
    let index = e.currentTarget.dataset.index;
    let imgArr = this.data.houseList[index].housesTypePicture;
    wx.previewImage({
      current: imgArr,     //当前图片地址
      urls: [imgArr],               //所有要预览的图片的地址集合 数组形式
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
  },

  getDetail: function () {
    req.get('/index/infoDetail.do', { keyId: this.data.keyId}).then(res => {
      let a = res.data.houses.housePicture.split(",")
      if (res.code == 1) {
        this.setData({
          movies: a,
        })
        this.setData({
          list: res.data.houses,
        }),
       this.setData({
          houseList: res.data.housesTypeList,
        })
        this.setData({
          messageList: res.data.housesMessageList,
        })
        wx.hideLoading();
      } else {
        app.showErrorMessage(res, '加载失败');
        wx.hideLoading();
        return;
      }
    })
  },

  onShareAppMessage: function (res) {
  // 　if(res.from == 'button'){
     const i = this.data.list;
     console.log(i)
     let shareObj = {
       title: `${i.houseName} 均价${i.perFlatPrice}元/平米 ${i.areaLabel}`,
       path: '/pages/detail/index?keyId=' + this.data.keyId + "&shareCustomerId=" + this.data.shareCustomerId,
       imageUrl: ''
     }
     return shareObj
// 　　}
  },

  getShareId: function() {
    req.get('/index/detail.do', { openId: app.globalData.openId }).then(res => {
      console.log(res)
      if (res.code == 1) {
        this.setData({
          shareCustomerId: res.data.shareCustomerId,
        })
        wx.hideLoading();
    
      } else {
        app.showErrorMessage(res, '加载失败');
        wx.hideLoading();
        return;
      }
    })
  },

  ask:function(e) {
    wx.makePhoneCall({
      phoneNumber: e.currentTarget.dataset.replyPhone,
    })
  },

  attention:function() {
    req.postFormData('/index/addCollect.do', {
      openId: app.globalData.openId,
      housesId: this.data.keyId,
    }).then(res => {
      if (res.code == 1) {
        this.setData({
          attentionStatus: true,
        })
        wx.showToast({
          title: '关注成功',
          icon: 'success'
        });
        wx.hideLoading();
      } else {
        wx.showToast({
          title: res.msg,
          icon: 'none'
        });
        wx.hideLoading();
        return;
      }
    })
  },

  unattention: function () {
    req.postFormData('/index/deleteCollect.do', {
      collectId: this.data.collectId,
    }).then(res => {
      if (res.code == 1) {
        this.setData({
          attentionStatus: false,
        })
        wx.showToast({
          title: '取消关注成功',
          icon: 'success'
        });
        wx.hideLoading();
      } else {
        wx.showToast({
          title: res.msg,
          icon: 'none'
        });
        wx.hideLoading();
        return;
      }
    })
  },

  getAttention: function () {
    req.get('/index/getCustomerCollect.do', {
      openId: app.globalData.openId,
      housesId: this.data.keyId,
    }).then(res => {
      console.log(res)
      if (res.code == 1) {
       if (res.data.keyId) {
         this.setData({
           collectId: res.data.keyId,
         })
         this.setData({
           attentionStatus: true,
         })
       }
      } else {
        wx.showToast({
          title: res.msg,
          icon: 'none'
        });
        return;
      }
    })
  },

  goMore:function() {
    wx.navigateTo({
      url: '../message/index?keyId=' + this.data.keyId
    })
  },

  goBack:function() {
    wx.reLaunch({
      url: '../index/index'
    })
  },

  goHouseType: function () {
    wx.navigateTo({
      url: '../houseType/index?keyId=' + this.data.keyId
    })
  },

  getPhoneNumber:function (e) {
    req.get('/ma/getPhoneNumber.do', { 
      encryptedData: e.detail.encryptedData, 
      ivStr: e.detail.iv, 
      sessionKey: app.globalData.sessionKey, 
      }).then(res => {
      if (res.code == 1) {
        wx.navigateTo({
          url: '../ask/index?keyId=' + this.data.keyId + '&phone=' + res.msg
        })
        wx.hideLoading();
      } else {
        app.showErrorMessage(res, '加载失败');
        wx.hideLoading();
        return;
      }
    }).catch(res => {
      wx.hideLoading();
      wx.showToast({
        title: '网络异常',
        icon: 'none'
      });
      console.log(res);
    });
  },

  openMap:function () {
    const latitude = Number(this.data.list.lat)
    const longitude = Number(this.data.list.lon)
    wx.openLocation({
      latitude,
      longitude,
      scale: 12
    })
  }

})